package com.zut.hospital.service.examination;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.examination.ProjectMapper;
import com.zut.hospital.dao.examination.ResultMapper;
import com.zut.hospital.dao.examination.Stu_Pro_RelationMapper;
import com.zut.hospital.dao.examination.StudentMapper;
import com.zut.hospital.entity.examination.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName:com.zut.hospital.service.examination
 * @Date:2019/5/17 17:30
 * @Author: zsy
 */
@Service
public class PhyexamServiceImpl implements PhyexamService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private Stu_Pro_RelationMapper stuProRelationMapper;
    @Autowired
    ResultMapper resultMapper;

    @Override
    public String layuiJsonParse(List<Map<String, String>> list,int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    @Override
    public void listLessRepeat(List list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
    }

    @Override
    public void addPerson(Integer deptId) {
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        criteria.andStuDepartIdEqualTo(deptId);
        List<Student> students = studentMapper.selectByExample(studentExample);
        ProjectExample projectExample = new ProjectExample();
        List<Project> projects = projectMapper.selectByExample(projectExample);
        Stu_Pro_Relation stuProRelation;
        for (Student student : students) {
            stuProRelation = new Stu_Pro_Relation();
            stuProRelation.setRelatStuId(student.getId());
            for (Project project : projects) {
                stuProRelation.setRelatProId(project.getId());
                stuProRelationMapper.insertSelective(stuProRelation);
            }
        }
    }

    @Override
    public void addOneResult(Integer stuId, Integer proId, String proResult) {
        Stu_Pro_RelationExample stuProRelationExample = new Stu_Pro_RelationExample();
        stuProRelationExample.or().andRelatStuIdEqualTo(stuId).andRelatProIdEqualTo(proId);
        List<Stu_Pro_Relation> stuProRelations = stuProRelationMapper.selectByExample(stuProRelationExample);
        Result result = new Result();
        result.setPhyRelationId(stuProRelations.get(0).getId());
        result.setPhyResult(proResult);
        result.setPhyQualify("null");
        result.setPhyRemark("null");
        resultMapper.insert(result);
    }
}
